<script type="text/javascript" src="/scripts/libs//vue-affix.min.js"></script>
<script type="text/javascript" src="/scripts/views/codeGen/constants.js"></script>

<#include "../components/cc-table-el.ftl"/>
<#include "components/cc-db-table-selector-el.ftl"/>
<#include "../components/cc-virtual-tab.ftl"/>
<#include "components/config-selector-dialog.ftl"/>
<#include "components/base/base-config-form.ftl"/>
<#include "components/base/policy-config-form.ftl"/>
<#include "components/base/table-config-form.ftl"/>
<#include "components/base/package-name-config-form.ftl"/>
<#include "components/base/class-name-config-form.ftl"/>
<#include "components/config-template-dialog.ftl"/>
<script type="text/x-template" id="code-gen-main">
    <div>
        <div class="action-list">
            <div style="padding: 10px;">
                <el-button type="primary" @click="execCodeGen">
                    生成代码
                </el-button>
                <el-button type="primary" @click="execCodeGenForOriginTable">
                    带副本表生成代码
                </el-button>
                <span style="color: red">(仅适用于带副本的原始表)</span>
            </div>
        </div>
        <el-tabs v-model="activeTab">
            <el-tab-pane label="基础配置" name="baseConfig">
                <base-config-form ref="baseConfigForm" :data="codeGenConfig"/>
            </el-tab-pane>
            <el-tab-pane label="表配置" name="tableConfig">
                <table-config-form ref="tableConfigForm" :data="codeGenConfig"/>
            </el-tab-pane>
            <el-tab-pane label="策略配置" name="policyConfig">
                <policy-config-form :data="codeGenConfig"/>
            </el-tab-pane>
            <el-tab-pane label="包名配置" name="packageNameConfig">
                <package-name-config-form :data="codeGenConfig"/>
            </el-tab-pane>
            <el-tab-pane label="类名配置" name="classNameConfig">
                <class-name-config-form :data="codeGenConfig"/>
            </el-tab-pane>
        </el-tabs>
    </div>
</script>

<script>
    Vue.component('code-gen-main', {
        template: '#code-gen-main',
        mixins: [window.ccMixIn.fixHeight],
        data() {
            return {
                activeTab: 'baseConfig',
                ulrs: {
                    getDetailInfo: '/codeGen/getDetailInfo',
                    save: '/codeGen/save',
                    execCodeGen: '/codeGen/execCodeGen',
                    downFile: '/codeGen/downFile?dir=',
                    configInfo: '/codeGen/configInfo',
                },
                codeGenConfig: {},
                config: []
            }
        },
        props: ['id'],
        computed: {},
        methods: {
            //生成代码
            execCodeGen: function () {
                this.validateFormData(function (err) {
                    if (!err) {
                        //验证通过
                        const data = this.getFormData()
                        this.ajax(this.ulrs.execCodeGen, data, function (result) {
                            if (result.success) {
                                this.$message({
                                    showClose: true,
                                    message: '生成成功',
                                    type: 'success'
                                })
                                if (this.codeGenConfig.localRunning === 0) {
                                    //非本地运行-服务器生成代码后,下载到本地
                                    window.open(this.ulrs.downFile + result.data);
                                }
                            } else {
                                this.$message(result.msg);
                            }
                        }.bind(this));
                    }
                }.bind(this))
            },
            execCodeGenForOriginTable(){
                this.validateFormData(function (err) {
                    if (!err) {
                        //验证通过
                        const data = this.getFormData(true)
                        this.ajax(this.ulrs.execCodeGen, data, function (result) {
                            if (result.success) {
                                this.$message({
                                    showClose: true,
                                    message: '生成成功',
                                    type: 'success'
                                })
                                if (this.codeGenConfig.localRunning === 0) {
                                    //非本地运行-服务器生成代码后,下载到本地
                                    window.open(this.ulrs.downFile + result.data);
                                }
                            } else {
                                this.$message(result.msg);
                            }
                        }.bind(this));
                    }
                }.bind(this))
            },
            //校验
            validateFormData(cb) {
                this.$refs.baseConfigForm.validateFormData(function (err) {
                    if (!err) {
                        this.$refs.tableConfigForm.validateFormData(function (err) {
                            if (!err) {
                                cb();
                            } else {
                                this.$message({
                                    type: 'error',
                                    message: '请选择表配置'
                                })
                                this.activeTab = 'tableConfig'
                                cb(true)
                            }
                        }.bind(this))
                    } else {
                        this.$message({
                            type: 'error',
                            message: '请填写基础配置'
                        })
                        this.activeTab = 'baseConfig'
                        cb(true)
                    }
                }.bind(this));
            },
            getFormData(originTableOnlyIs) {
                let saveData = {};
                if (originTableOnlyIs){
                    saveData.originTableOnly=1
                }
                saveData.id = this.codeGenConfig.id
                saveData.author = this.codeGenConfig.author
                saveData.localRunning = this.codeGenConfig.localRunning
                saveData.outputDir = this.codeGenConfig.outputDir
                saveData.useProjectPath = this.codeGenConfig.useProjectPath
                saveData.openDirAfterDone = this.codeGenConfig.openDirAfterDone
                saveData.dataSourceConfig = this.codeGenConfig.dataSourceConfig
                saveData.packageConfig = this.codeGenConfig.packageConfig
                saveData.classNameConfig = this.codeGenConfig.classNameConfig
                saveData.strategyConfig = this.codeGenConfig.strategyConfig
                const tableList = this.codeGenConfig.tableList || []
                const tableList2Back = []
                tableList.forEach(function (item, index) {
                    tableList2Back.push(Object.assign({}, item.data))
                })
                saveData.tableList = tableList2Back
                let jsonStr = JSON.stringify(saveData);
                const data = {jsonStr: jsonStr}
                return data
            },
            setFormData: function (editData) {


            }
        },
        created() {
            this.codeGenConfig = CODE_GEN_CONFIG;
            this.codeGenConfig.tableConfigList = TABLE_CONFIG_LIST
        }
    })
</script>
<style>
    .action-list .el-form-item {
        margin-bottom: 0px
    }

    .legend-title {
        cursor: pointer;
        line-height: 30px;
        padding-left: 5px;
    }

    .legend-title span {
        float: none;
    }
</style>
